﻿using System;
using System.Collections.Generic;
using System.Web;


namespace CCPSIP.User
{
    /// <summary>CCPSIP_035：Device UUID 變更 API</summary>
    /// OldUUID = 舊的裝置 UUID。
    /// NewUUID = 新的裝置 UUID。
    /// DeviceToken = Device Push Token。
    /// DevEnv = 開發測試版為'Y'，正式發行版為'N'。
    /// DeviceType = 裝置類型代號。

    public class uuid_change : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";


            try
            {
                //【01】取得POST參數
                //
                string OldUUID = req.Form["OldUUID"];//舊的UUID
                string NewUUID = req.Form["NewUUID"];//新的UUID
                string DeviceToken = req.Form["DeviceToken"];//Device Push Token
                string DevEnv = req.Form["DevEnv"];//是否為開發測試版
                string DeviceType = req.Form["DeviceType"];//裝置類型

                try
                {
                    if (NewUUID.Length < 15)
                        throw new Exception();

                    DeviceToken = DeviceToken.Trim();
                }
                catch
                {
                    throw new Exception("Error_01: Parameter Error");//參數錯誤
                }

                string PushType = PushKit.ToPushType(DevEnv, DeviceToken);


                //【02】檢查UUID + DeviceToken是否唯一
                //
                bool isUniqueToken = OracleKitUser.isUniqueToken(OldUUID, DeviceToken, PushType);
                if (!isUniqueToken)
                {
                    throw new Exception("Error_02: Cannot identify this token");//無法識別此Token
                }


                //【03】更改裝置的UUID
                //
                string SIP_Num = OracleKitUser.UpdateDeviceUUID(OldUUID, NewUUID, DeviceToken, PushType, DeviceType);
                if (string.IsNullOrEmpty(SIP_Num))
                {
                    throw new Exception("Error_03: Change UUID failed");//更改UUID失敗
                }


                //【04】更改IP PBX登入密碼
                //
                Dictionary<string, string> paramDict = IPPbxKitV3.GetUserData(SIP_Num);
                paramDict["secret"] = SipKit.calcSipSecret(SIP_Num);//註冊密碼
                paramDict["password"] = paramDict["secret"];//網頁密碼
                IPPbxKitV3.UpdateUser(paramDict);


                //【99】回傳新的UUID
                //
                resultStr = NewUUID;
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
